layui.define(['table', 'form','table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,table = layui.table;
  
  $('#userId').select2({
      placeholder: "请选择设备归属人",
      width: '250px',
      theme: 'bootstrap',
      allowClear : true,
      ajax: {
          url: layui.setter.webRoot + '/agent/select.data',
          method: 'post',
          delay: 250,
          processResults: function (result) {
              if (result.code == 200) {
                  return {
                      pagination: {more: result.body.more},
                      results: result.body.data || []
                  };
              } else {
                  return {results: []};
              }
          }
      }
  });

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/bxscn/device/portable_battery/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'deviceSn', title: '设备序列号'}
      ,{field: 'typeName', title: '设备类型'}
      ,{field: 'batteryPower', title: '电池功率'}
      ,{field: 'leaseTypeName', title: '租借方式'}
      , {field: 'userId', title: '设备所属人', templet: function (record) {
            if(record.userId){
                return record.userName + '('+record.userId+')';
            }else{
                return '无';
            }
              }}
      ,{field: 'placeId', title: '部署',templet:function (record){
          if(record.cabinetDeviceSn){
              return '机柜：' + record.cabinetDeviceSn
                  + "，卡槽：" + record.slotNum
                  + "，电量：" + record.power;
          }else if(record.placeId){
              return '网点：' + record.placeName
                  + "("+record.placeId+")"
                  + "，地区：" + record.areaName;
          }else {
              return '未部署';
          }
      }}
      ,{field: 'stateName', title: '状态'}
      ,{field: 'currentTradeNo', title: '当前订单',templet:function (record){
          if(record.currentTradeNo) {
              return '[' + record.leaseTypeName + ']' + record.currentTradeNo;
          }else{
              return '---';
          }
              }}
      ,{field: 'lastLeaseDate', title: '最后租借'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 200, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add: function(){
          layer.open({
              type: 2
              ,title: '新增便携式储能电池'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/add.html'
              ,maxmin: true
              ,area: ['500px', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,info:function(obj){
    	layer.open({
            type: 2
            ,title: '新增便携式储能电池详情'
            ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/info.html?deviceSn='+obj.data.deviceSn
            ,maxmin: true
            ,area: ['90%', '90%']
        });
    }
    ,belong:function (obj){
          layer.open({
              type: 2
              ,title: '设置归属人'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/belong.html?deviceSn='+obj.data.deviceSn
              ,maxmin: true
              ,area: ['500px', '600px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/belong.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,del:function(obj){
    	layer.confirm('确定要删除该便携式储能电池吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/bxscn/device/portable_battery/del.do',
	          data:{deviceSn:obj.data.deviceSn},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('portable_energy_battery_list', {})
});